<template>
  <div class="app-container">
    <eHeader :param-bean="paramBean" :dicts="dicts" :sup_this="sup_this"/>
    <!--表格渲染-->
    <tree-table v-loading="loading" :expand-all="expand" :data="data" :columns="columns" size="small">
      <el-table-column prop="icon" label="图标" align="center" width="80px">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column prop="sort" align="center" width="100px" label="排序">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.sort }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="pathUrl" label="Url"/>
      <el-table-column :show-overflow-tooltip="true" prop="componentUrl" label="组件路径"/>
      <el-table-column label="菜单类型" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.menu_type" :key="item.id">
            <el-tag v-if="scope.row.menuType.toString() === item.dictCode">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否管理员" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.yesorno" :key="item.id">
            <el-tag v-if="scope.row.admin.toString() === item.dictCode">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['SYS_ADMIN','MENU_ALL','MENU_EDIT','MENU_DELETE'])" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <edit v-permission="['SYS_ADMIN','MENU_ALL','MENU_EDIT']" :dicts="dicts" :data="scope.row" :sup_this="sup_this"/>
          <el-popover
            v-permission="['SYS_ADMIN','MENU_ALL','MENU_DELETE']"
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" :disabled="scope.row.id === 1" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </tree-table>
  </div>
</template>

<script>
import treeTable from '@/components/TreeTable'
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { del, loadDataUrl } from '@/api/menu'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'

export default {
  components: { eHeader, edit, treeTable },
  mixins: [initData, initDict],
  data() {
    return {
      columns: [
        {
          text: '名称',
          value: 'menuName'
        }
      ],
      delLoading: false, sup_this: this, expand: true
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    async initDataDict() {
      await this.getDictByType('menu_type')
      await this.getDictByType('yesorno')
    },
    beforeInit() {
      this.url = loadDataUrl()
      this.pageCount = false
      if (!this.dicts.menu_type) {
        this.initDataDict()
      }
      return true
    },
    subDelete(id) {
      this.delLoading = true
      const data = { idList: [{ id: id }] }
      del(data).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
